package com.fh.db.oracle;

import com.fh.db.DbSchema;
import com.fh.db.base.BaseDbSource;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @Author qiujunda
 * @Date 16/05/2020
 * @Description
 */
public class OracleDbSource extends BaseDbSource {
    public OracleDbSource() {
        setSqlSet(new OracleSqlSet());
    }

    @Override
    public String getPageSql(String sql, int offset, int limit) {
        int start = offset * limit;
        int end = start + limit;
        StringBuilder sqlBuilder = new StringBuilder(sql.length() + 120);
        sqlBuilder.append("SELECT * FROM ( ");
        sqlBuilder.append(" SELECT TMP_PAGE.*, ROWNUM ROW_ID FROM ( ");
        sqlBuilder.append(sql);
        sqlBuilder.append(" ) TMP_PAGE)");
        sqlBuilder.append(" WHERE ROW_ID <= " + end + " AND ROW_ID > " + start + "");
        return sqlBuilder.toString();
    }

    @Override
    public DbSchema getSchema() {
        OracleDbSchema oracleDbSchema = new OracleDbSchema();
        oracleDbSchema.setDbSource(this);
        return oracleDbSchema;
    }

    @Override
    public Connection getConnection() throws ClassNotFoundException, SQLException {
        Class.forName(getDriver());
        Connection connection = DriverManager.getConnection(getUrl(), getUserName(), getPassword());
        return connection;
    }

}
